/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.aseclipse.core.runtime.registry.classes {

import flash.utils.getQualifiedClassName;

/**
 * An object which has the general characteristics of all elements
 * in a plug-in manifest.
 * <p>
 * This class may be subclassed.
 * </p>
 */

public class RegistryModelObject {

	// DTD properties (included in plug-in manifest)
	private var name:String = null;

	// transient properties (not included in plug-in manifest)
	private var flags:int = 0;
	// the last bit is a read-only flag
	// IMPORTANT: One bit in the "flags" integer is used to store the 
	// read-only flag and the other bits are used to store an integer value
	// which can be from -1 to (2**31) - 1. To help with the bit masking, the integer
	// value stored in "flags" is (value + 1). This means that a "flags" value
	// of 0 will NOT be marked as read-only and will return -1 for the start line value.
	private static const M_READ_ONLY:Number = 0x80000000;

	private var parent:RegistryModelObject;

	/**
	 * Checks that this model object is writeable.  A runtime exception
	 * is thrown if it is not.
	 */
	protected function assertIsWriteable():void {
		// Assert.isTrue(!isReadOnly(), "Model is read-only"); //$NON-NLS-1$
		if(!isReadOnly()) {
		    //throw new Error("Model is read-only");
		}
	}
	/**
	 * Returns the name of this element.
	 * 
	 * @return the name of this element or <code>null</code>
	 */
	public function getName():String {
		return name;
	}
	/**
	 * Returns whether or not this model object is read-only.
	 * 
	 * @return <code>true</code> if this model object is read-only,
	 *		<code>false</code> otherwise
	 * @see #markReadOnly
	 */
	public function isReadOnly():Boolean {
		return (flags & M_READ_ONLY) == M_READ_ONLY;
	}
	/**
	 * Sets this model object and all of its descendents to be read-only.
	 * Subclasses may extend this implementation.
	 *
	 * @see #isReadOnly
	 */
	public function markReadOnly():void { 
		flags |= M_READ_ONLY;
	}
	/**
	 * Sets the name of this element.
	 * 
	 * @param value the new name of this element.  May be <code>null</code>.
	 */
	public function setName(value:String):void {
		assertIsWriteable();
		name = value;
	}
	
	/**
	 * Returns the plug-in model (descriptor or fragment) in which this extension is declared.
	 *
	 * @return the plug-in model in which this extension is declared
	 *  or <code>null</code>
	 */
	public function getParent():Object {
		return parent;
	}
	/**
	 * Sets the plug-in model in which this extension is declared.
	 * This object must not be read-only.
	 *
	 * @param value the plug-in model in which this extension is declared.  
	 *		May be <code>null</code>.
	 */
	public function setParent(value:RegistryModelObject):void {
		assertIsWriteable();
		this.parent = value;
	}
	/**
	 * Return a string representation of this object. This value is not to be relied
	 * on and can change at any time. To be used for debugging purposes only.
	 * 
	 * @see java.lang.Object#toString()
	 */
	public function toString():String {
		return getQualifiedClassName(this) + ":" + getName() + "[" + super.toString() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
	
	public function getAdapter(type:Class):Object {
		return null; //InternalPlatform.getDefault().getAdapterManager().getAdapter(this, type);
	}

	public function getRegistry():RegistryModelObject {
		return parent == null ? this : parent.getRegistry();
	}
}
}